var xmlhttp;
var price;
function getPrice(str) {
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) {
		alert("Browser does not support HTTP Request");
		return;
	}
	var e = document.getElementById('Bill_product_id');
	var productid = e.options[e.selectedIndex].value;
	var url = "index.php?r=bill/price/id/" + productid;
	// url+="?r="+str;
	// url=url+"&sid="+Math.random();
	xmlhttp.onreadystatechange = productChanged;
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function productChanged() {
	if (xmlhttp.readyState == 4) {
		document.getElementById("_price").innerHTML = xmlhttp.responseText;
		price = xmlhttp.responseText;
		var money = price * document.getElementById("Bill_amount").value;
		document.getElementById("_money").innerHTML = money;
		document.getElementById("Bill_money").value = money;
		document.getElementById("Customer_c_totalmoney").value = money;
		document.getElementById("_totalMoney").innerHTML = money;

	}
}

function amountchanged() {
	var money = price * document.getElementById("Bill_amount").value;
	document.getElementById("_money").innerHTML = money;
	document.getElementById("Bill_money").value = money;
	document.getElementById("Customer_c_totalmoney").value = money;
	document.getElementById("_totalMoney").innerHTML = money;
}

var j = 0;
function addproduct() {
	var shopcart=document.getElementById("shopcart");
	
	var productID=document.getElementById("Bill_product_id").options[document.getElementById("Bill_product_id").selectedIndex].value;
	var p_id=document.createElement("input");
	p_id.setAttribute("type", "hidden");
	p_id.setAttribute("id", "Bill[product_id]["+j+"]");
	p_id.setAttribute("name", "Bill[product_id]["+j+"]");
	p_id.setAttribute("value", productID);
	
	var productName=document.getElementById("Bill_product_id").options[document.getElementById("Bill_product_id").selectedIndex].innerHTML;
	var p_name=document.createElement("label");
	p_name.innerHTML = productName;
	
	//Check exits of product.
	var productExits=false;
	for (i=0;i<j;i++){
		var len = document.getElementById("Bill[product_id]["+i+"]").value;
		if(len==productID){
			productExits=true;
			break;
		}
	}

	//update shopcart for exits  product
	if (productExits) {
		price=parseInt(price,10);
		var oldvalue = parseInt(document.getElementById("lblBill[amount][" + i + "]").innerHTML, 10);
		var morequantity = parseInt(document.getElementById("Bill_amount").options[document
				.getElementById("Bill_amount").selectedIndex].value,10);
		document.getElementById("lblBill[amount][" + i + "]").innerHTML = oldvalue +  morequantity;
		document.getElementsByName("Bill[amount][" + i + "]")[0].value = oldvalue +  morequantity ;
		oldvalue = parseInt(document.getElementById("lblBill[money][" + i + "]").innerHTML,10);
		document.getElementById("lblBill[money][" + i + "]").innerHTML = oldvalue + (price * morequantity);
		document.getElementsByName("Bill[money][" + i + "]")[0].value = oldvalue +  (price * morequantity);
	}else{
		
		//Add one more product
		var unitPrice=document.getElementById("_price").innerHTML;
		var p_unitPrice=document.createElement("label");
		p_unitPrice.setAttribute("id", "Bill[price]["+j+"]");
		p_unitPrice.innerHTML =  unitPrice;
		
		var quantity=document.getElementById("Bill_amount").options[document.getElementById("Bill_amount").selectedIndex].value;
		var p_quantity=document.createElement("label");
		p_quantity.setAttribute("id", "lblBill[amount]["+j+"]");
		p_quantity.setAttribute("name", "lblBill[amount]["+j+"]");
		p_quantity.innerHTML =  quantity;
		
		var p_quantityValue=document.createElement("input");
		p_quantityValue.setAttribute("type", "hidden");
		p_quantityValue.setAttribute("name", "Bill[amount]["+j+"]");
		p_quantityValue.setAttribute("value", quantity);
		
		var money=document.getElementById("_money").innerHTML;
		var p_money=document.createElement("label");
		p_money.setAttribute("id", "lblBill[money]["+j+"]");
		p_money.innerHTML = money;
		
		shopcart.appendChild(p_id);
		shopcart.appendChild(p_name);
		shopcart.appendChild(p_unitPrice);
		shopcart.appendChild(p_quantity);
		shopcart.appendChild(p_quantityValue);
		shopcart.appendChild(p_money);
		
		j++;
	}
}

function GetXmlHttpObject() {
	if (window.XMLHttpRequest) {
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject) {
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}